package handler

import (
	"github.com/gin-gonic/gin"
	"red-book-manager/models"
	"red-book-manager/pkg"
	"strconv"
)

func Login(c *gin.Context) {
	tel := c.PostForm("tel")
	password := c.PostForm("password")
	var user models.User
	err := models.DB.Where("tel = ?", tel).First(&user).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "没有找到该电话号码",
		})
		return
	}

	if user.Password != pkg.Md5Str(password) {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "密码不正确",
		})
		return
	}

	token := pkg.JwtHandle(strconv.Itoa(int(user.ID)))

	c.JSON(200, gin.H{
		"code":  200,
		"msg":   "登录成功",
		"token": token,
	})
	return

}
